/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.editor.view;

import java.awt.Color;
import java.io.IOException;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Element;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLWriter;

public class StyledHTMLWriter extends HTMLWriter {
	StyledDocumentWriter writer;
    protected static Color COLOR_TEXT = Color.BLACK;
    protected static Color COLOR_ELEMENT = Color.BLUE;
    protected static Color COLOR_ATTRIBUTE = Color.GRAY;
    protected static Color COLOR_NORMAL = Color.BLACK;
	
	public StyledHTMLWriter(StyledDocumentWriter w, HTMLDocument doc, int pos, int len) {
		super(w, doc, pos, len);
		writer = w;
	}

	public StyledHTMLWriter(StyledDocumentWriter w, HTMLDocument doc) {
		super(w, doc);
		writer = w;
	}

	protected void endTag(Element elem) throws IOException {
		StyleConstants.setForeground(writer.getAttributes(), COLOR_ELEMENT );
		super.endTag(elem);
		StyleConstants.setForeground(writer.getAttributes(), COLOR_NORMAL );
	}

	protected void startTag(Element elem) throws IOException, BadLocationException {
		StyleConstants.setForeground(writer.getAttributes(), COLOR_ELEMENT );
		super.startTag(elem);
		StyleConstants.setForeground(writer.getAttributes(), COLOR_NORMAL );
	}

	protected void text(Element elem) throws BadLocationException, IOException {
		StyleConstants.setForeground(writer.getAttributes(), COLOR_TEXT );
		super.text(elem);
		StyleConstants.setForeground(writer.getAttributes(), COLOR_NORMAL );
	}

    protected void emptyTag(Element elem) throws BadLocationException, IOException {
		StyleConstants.setForeground(writer.getAttributes(), COLOR_ELEMENT );
		super.emptyTag(elem);
		StyleConstants.setForeground(writer.getAttributes(), COLOR_NORMAL );
	}

	protected void writeAttributes(AttributeSet attr) throws IOException {
		StyleConstants.setForeground(writer.getAttributes(), COLOR_ATTRIBUTE );
		super.writeAttributes(attr);
		StyleConstants.setForeground(writer.getAttributes(), COLOR_ELEMENT );
	}
}
